<template>
  <header class="header">
    <div class="header-top">
      <div class="header-warp">
        <div class="header-user">
          <span>尚品汇欢迎您！</span>
          <span v-if="name">
            <span>{{ name }}</span>
            <button @click="handleLogout">退出登录</button>
          </span>

          <span v-else
            >请 <router-link to="/login">登录</router-link> |
            <router-link to="/register">免费注册</router-link></span
          >
        </div>
        <ul class="header-nav">
          <li><router-link to="/center">我的订单</router-link></li>
          <li><router-link to="/shopcart">我的购物车</router-link></li>
          <li>我的尚品汇</li>
          <li>尚品汇会员</li>
          <li>企业采购</li>
          <li>关注尚品汇</li>
          <li>合作招商</li>
          <li>商家后台</li>
        </ul>
      </div>
    </div>
    <div class="header-buttom header-warp">
      <img src="./img/logo.png" alt="" class="header-logo" />
      <form class="header-form" @submit.prevent="toSearch">
        <input type="text" class="header-input" v-model="keyword" />
        <button class="header-btn">搜索</button>
      </form>
    </div>
  </header>
</template>

<script>
import { mapActions, mapState } from 'vuex'
export default {
  name: 'XHeader',
  data() {
    return {
      // 一上来就有数据
      // keyword: this.$route.query.keyword,
      keyword: '',
    }
  },
  methods: {
    ...mapActions('user', ['logout']),
    async handleLogout() {
      await this.logout()
      //退出登录之后要跳回首页
      this.$router.push('/')
    },
    toSearch() {
      const keyword = this.keyword
      const query = { ...this.$route.query }
      // 删除keyword属性,解决 手动把输入框的keyword删掉的话，地址栏里面还是会显示keyword
      delete query.keyword

      // 如果传了keyword，就给query添加keyword
      if (keyword) {
        query.keyword = keyword
      }

      this.$router.push({
        name: 'Search',
        query,
      })
    },
  },
  watch: {
    // $route(route) {
    //   // 监听路由 当路由发生改变时，给关键字重新赋值
    //   this.keyword = route.query.keyword
    // },
    $route: {
      handler(route) {
        this.keyword = route.query.keyword
      },
      immediate: true,
    },
  },
  computed: {
    ...mapState({
      name: (state) => state.user.name,
    }),
  },
}
</script>

<style lang="less" scoped>
.header-top {
  height: 30px;
  background-color: #eaeaea;
}
.header-warp {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 1200px;
  height: 100%;
  margin: 0 auto;
}
.header-nav {
  display: flex;
}
.header-nav li {
  border-right: 1px solid #b7b2b2;
  padding-right: 10px;
  margin-right: 10px;
  cursor: pointer;
}
.header-nav :last-child {
  border-right: none;
  padding-right: 0;
  margin-right: 0;
}
.header-buttom {
  height: 100px;
}
.header form {
  display: flex;
  align-items: center;
}
.header input {
  width: 500px;
  height: 30px;
  border: 2px solid #ea4a36;
  outline: none;
  padding: 0 5px;
}
.header-btn {
  width: 80px;
  height: 34px;
  border: none;
  background-color: #ea4a36;
  color: #fff;
  cursor: pointer;
}
</style>
